/**
 * Copyright (c) 2011 eelpo.com
 * All rights reserved.
 */
package com.eelpo.cmmp.core.wicket.application.setting;

import com.eelpo.cmmp.core.wicket.application.authorization.CMMPAuthorizationStrategy;
import com.eelpo.cmmp.core.wicket.page.error.AccessDeniedPage;
import com.eelpo.cmmp.core.wicket.page.error.InternalErrorPage;

import org.apache.wicket.Application;
import org.apache.wicket.Page;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <pre>
 * 
 * 提供基础设置，预留扩展
 * 
 * </pre>
 * 
 * @author <a href="mailto:Brian@eelpo.com">汪洪州</a>
 * @since 1.0
 */
public abstract class CoreSettings implements Settings {

	/**
	 * 日志管理器
	 */
	private static final Logger logger = LoggerFactory.getLogger(CoreSettings.class);

	@Override
	public void settings(WebApplication webApplication) {

		// 设置Session页面缓存的最大值
		webApplication.getSessionSettings().setMaxPageMaps(getMaxPageMaps());
		logger.info("setMaxPageMaps({})", getMaxPageMaps());

		// 设置Wicket页面输出的编码方式
		webApplication.getMarkupSettings().setDefaultMarkupEncoding(getDefaultMarkupEncoding());
		logger.info("setDefaultMarkupEncoding('{}')", getDefaultMarkupEncoding());

		// 设置请求和响应的编码方式
		webApplication.getRequestCycleSettings().setResponseRequestEncoding(getResponseRequestEncoding());
		logger.info("setResponseRequestEncoding('{}')", getResponseRequestEncoding());

		// 设置访问限制的跳转页面
		webApplication.getApplicationSettings().setAccessDeniedPage(getAccessDeniedPage());
		logger.info("setAccessDeniedPage({})", getAccessDeniedPage());

		// 设置内部错误的跳转页面
		webApplication.getApplicationSettings().setInternalErrorPage(getInternalErrorPage());
		logger.info("setInternalErrorPage({})", getInternalErrorPage());

		// 设置页面过期的跳转页面
		webApplication.getApplicationSettings().setPageExpiredErrorPage(getPageExpiredErrorPage());
		logger.info("setPageExpiredErrorPage({})", getPageExpiredErrorPage());

		// 判断用户是否登陆，或者Session是否过期
		webApplication.getSecuritySettings().setAuthorizationStrategy(getAuthorizationStrategy());
		logger.info("setAuthorizationStrategy({})", getAuthorizationStrategy());

		// 判断用户是否登陆，或者Session是否过期
		webApplication.getSecuritySettings().setAuthorizationStrategy(getAuthorizationStrategy());
		logger.info("setAuthorizationStrategy({})", getAuthorizationStrategy());

		// 开发模式记录请求日志
		if (Application.DEVELOPMENT.equalsIgnoreCase(webApplication.getConfigurationType())) {
			webApplication.getRequestLoggerSettings().setRecordSessionSize(true);
			webApplication.getRequestLoggerSettings().setRequestLoggerEnabled(true);
		}

		// 注入Spring容器组件
		webApplication.addComponentInstantiationListener(new SpringComponentInjector(webApplication));
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.core.wicket.application.setting.Settings#getMaxPageMaps()
	 */
	@Override
	public int getMaxPageMaps() {
		return 100;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.core.wicket.application.setting.Settings#
	 *      getDefaultMarkupEncoding()
	 */
	@Override
	public String getDefaultMarkupEncoding() {
		return "UTF-8";
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.core.wicket.application.setting.Settings#
	 *      getResponseRequestEncoding()
	 */
	@Override
	public String getResponseRequestEncoding() {
		return "UTF-8";
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.core.wicket.application.setting.Settings#getAccessDeniedPage
	 *      ()
	 */
	@Override
	public Class<? extends Page> getAccessDeniedPage() {
		return AccessDeniedPage.class;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.core.wicket.application.setting.Settings#getInternalErrorPage
	 *      ()
	 */
	@Override
	public Class<? extends Page> getInternalErrorPage() {
		return InternalErrorPage.class;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.core.wicket.application.setting.Settings#
	 *      getPageExpiredErrorPage()
	 */
	@Override
	public Class<? extends Page> getPageExpiredErrorPage() {
		return AccessDeniedPage.class;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see com.eelpo.cmmp.core.wicket.application.setting.Settings#
	 *      getAuthorizationStrategy()
	 */
	@Override
	public IAuthorizationStrategy getAuthorizationStrategy() {
		CMMPAuthorizationStrategy authorizationStrategy = new CMMPAuthorizationStrategy();
		authorizationStrategy.setLoginPage(getLoginPage());
		return authorizationStrategy;
	}
}
